package com.ft.FileTest.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.web.multipart.MultipartFile;

public class FastDFSUtil {
	private static byte[] getFileBuffer(InputStream inStream, long fileLength) throws IOException {

		byte[] buffer = new byte[256 * 1024];
		byte[] fileBuffer = new byte[(int) fileLength];

		int count = 0;
		int length = 0;

		while ((length = inStream.read(buffer)) != -1) {
			for (int i = 0; i < length; ++i) {
				fileBuffer[count + i] = buffer[i];
			}
			count += length;
		}
		return fileBuffer;
	}
	
	public static String uploadFileToFDFS(MultipartFile multipartFile) throws Exception {
		String classPath = new File(FastDFSUtil.class.getResource("/").getFile()).getCanonicalPath();
		String configFilePath = classPath + File.separator + "fdfs_client.conf";
		configFilePath = URLDecoder.decode(configFilePath, "utf-8");
		System.out.println("配置文件:" + configFilePath);

		ClientGlobal.init(configFilePath);

		try {
			String uploadFileName = multipartFile.getOriginalFilename();
			InputStream inputStream = multipartFile.getInputStream();
			long fileLength = multipartFile.getBytes().length;
			byte[] fileBuff = getFileBuffer(inputStream, fileLength);
			String fileId = "";
			String fileExtName = "";
			if (uploadFileName.contains(".")) {
				fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
			} else {
				System.out.println("Fail to upload file, because the format of filename is illegal.");
				return fileId;
			}

			// 建立连接
			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;
			StorageClient1 client = new StorageClient1(trackerServer, storageServer);

			// 设置元信息
			NameValuePair[] metaList = new NameValuePair[3];
			metaList[0] = new NameValuePair("fileName", uploadFileName);
			metaList[1] = new NameValuePair("fileExtName", fileExtName);
			metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));

			try {
				fileId = client.upload_file1(fileBuff, fileExtName, metaList);
			} catch (Exception e) {
				System.out.println("Upload file \"" + uploadFileName + "\"fails");
			}
			trackerServer.close();
			System.out.println(fileId);
			return fileId;
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return null;
	}
	
	public static byte[] downLoadFile(String fileid) {
		try {

			String classPath = new File(FastDFSUtil.class.getResource("/").getFile()).getCanonicalPath();
			String configFilePath = classPath + File.separator + "fdfs_client.conf";
			configFilePath = URLDecoder.decode(configFilePath, "utf-8");
			System.out.println("配置文件:" + configFilePath);

			ClientGlobal.init(configFilePath);
			// 建立连接
			TrackerClient tracker = new TrackerClient();
			TrackerServer trackerServer = tracker.getConnection();
			StorageServer storageServer = null;
			StorageClient1 client = new StorageClient1(trackerServer, storageServer);
			byte[] filebyte = client.download_file1(fileid);
			trackerServer.close();

			return filebyte;
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		return null;

	}
}
